<template>
  <div class="terminal-container">
    <div ref="terminal" class="terminal">
      <div class="output-container">
        <div v-for="(line, index) in output" :key="index" class="line">
          <span v-if="line.type === 'input'" class="prompt">$</span>
          <span :class="line.type">{{ line.text }}</span>
        </div>
      </div>
      <div class="input-line">
        <span class="prompt">$</span>
        <input
          v-model="currentInput"
          @keyup.enter="sendCommand"
          @keyup.up="historyUp"
          @keyup.down="historyDown"
          ref="input"
          class="input"
          id="terminal-input"
          :disabled="false"
          :readonly="false"
          autofocus
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick, watch } from 'vue'
import { useWebSocket } from '@vueuse/core'

const output = ref([])
const currentInput = ref('')
const input = ref(null)
const terminal = ref(null)
const commandHistory = ref([])
const historyIndex = ref(-1)

// 模拟WebSocket连接
const mockWebSocket = {
  send: (cmd) => {
    console.log('Mock sending command:', cmd)
    setTimeout(() => {
      addOutput(`Processed: ${cmd}`, 'output')
    }, 300)
  }
}

const send = (cmd) => mockWebSocket.send(cmd)
const data = ref(null)

onMounted(() => {
  focusInput()
  addOutput('Welcome to Web Terminal', 'system')
})

watch(data, (newData) => {
  if (newData) {
    addOutput(newData, 'output')
  }
})

const focusInput = () => {
  nextTick(() => {
    if (input.value) {
      input.value.focus()
      if (terminal.value) {
        terminal.value.addEventListener('click', () => {
          input.value.focus()
        })
      }
    }
  })
}

const addOutput = (text, type = 'output') => {
  output.value.push({ text, type })
  scrollToBottom()
}

const scrollToBottom = () => {
  nextTick(() => {
    if (terminal.value) {
      terminal.value.scrollTop = terminal.value.scrollHeight
    }
  })
}

const sendCommand = () => {
  if (!currentInput.value.trim()) {
    currentInput.value = ''
    focusInput()
    return
  }

  addOutput(currentInput.value, 'input')
  send(currentInput.value)
  
  commandHistory.value.push(currentInput.value)
  historyIndex.value = commandHistory.value.length
  
  currentInput.value = ''
  focusInput()
}

const historyUp = () => {
  if (commandHistory.value.length === 0) return
  
  if (historyIndex.value > 0) {
    historyIndex.value--
    currentInput.value = commandHistory.value[historyIndex.value]
  }
}

const historyDown = () => {
  if (historyIndex.value < commandHistory.value.length - 1) {
    historyIndex.value++
    currentInput.value = commandHistory.value[historyIndex.value]
  } else {
    historyIndex.value = commandHistory.value.length
    currentInput.value = ''
  }
}
</script>

<style scoped>
.terminal-container {
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.terminal {
  width: 100%;
  flex: 1;
  padding: 0;
  background-color: #000;
  overflow: auto;
  display: flex;
  flex-direction: column;
  position: relative;
}

.output-container {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
  margin: 0;
  padding-bottom: 10px;
}

.line {
  margin: 0;
  padding: 0;
  white-space: pre-wrap;
  word-break: break-all;
  line-height: 1.2;
}

.input-line {
  display: flex;
  align-items: center;
  margin: 0;
  padding: 10px;
  background-color: #000;
  position: sticky;
  top: 0;
  z-index: 10;
}

.prompt {
  color: #4CAF50;
  margin-right: 10px;
}

.input {
  background-color: transparent;
  border: none;
  color: #f0f0f0;
  font-family: monospace;
  flex-grow: 1;
  outline: none;
  caret-color: #4CAF50;
  width: 100%;
  padding: 0 5px;
}

.output {
  color: #f0f0f0;
}

.system {
  color: #888;
}

.error {
  color: #f44336;
}
</style>
